package com.minjiang.blog.service.impl;

import com.minjiang.blog.exception.NotFundException;
import com.minjiang.blog.mapper.TypeDao;
import com.minjiang.blog.mode.Type;
import com.minjiang.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Transactional 保证事务的一致性
 */
@Service
@SuppressWarnings("all")
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeDao typeDao;

    @Transactional
    @Override
    public int saveType(Type type) {
        return typeDao.saveType(type);
    }

    @Transactional
    @Override
    public Type findType(Long id) {
        return typeDao.findType(id);
    }

    @Transactional
    @Override
    public List<Type> findAllType() {
        return typeDao.findAllType();
    }

    @Transactional
    @Override
    public List<Type> findAllTypeAndBlog() {
        return typeDao.findAllTypeAndBlog();
    }

    @Transactional
    @Override
    public Type getTypeByName(String name) {
        return typeDao.getTypeByName(name);
    }

    @Transactional
    @Override
    public int updateType(Type type) {
        int i = typeDao.updateType(type);
        if (i <0){
            throw new NotFundException();
        }
        return i;
    }

    @Transactional
    @Override
    public int deleteType(Long id) {
        int i = typeDao.deleteType(id);
        if (i <0){
            throw new NotFundException();
        }
        return i;
    }
}
